package com.example.xiaxiamusicplayer;

import android.os.Bundle;
import android.os.CountDownTimer;
import android.view.View;
import android.view.animation.Animation;
import android.view.animation.LinearInterpolator;
import android.view.animation.RotateAnimation;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.SeekBar;
import android.widget.TextView;

import androidx.annotation.Nullable;
import androidx.appcompat.app.AppCompatActivity;

import com.bumptech.glide.Glide;
import com.example.xiaxiamusicplayer.bean.Mp3;
import com.example.xiaxiamusicplayer.utils.Mp3Manage;
import com.example.xiaxiamusicplayer.utils.XiaXiaPlayer;

public class MusicDetailActivity extends AppCompatActivity {
    private String imageUrl = "https://gimg2.baidu.com/image_search/src=http%3A%2F%2Fdl.yzz.cn%2Fpublic%2Fimages%2F100406%2F29_184534_2.jpg&refer=http%3A%2F%2Fdl.yzz.cn&app=2002&size=f9999,10000&q=a80&n=0&g=0n&fmt=jpeg?sec=1623146140&t=106405d49a68a256a6cf13e9e260295c";
    private ImageView imageView;
    private TextView tvName, tvSinger, tvPlayTime, tvAllTime;
    private Button btLast, btPlay, btNext;
    private SeekBar seekBar;
    private CountDownTimer countDownTimer;
    private boolean isTrackingTouch;

    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_detail);
        imageView = findViewById(R.id.image_view);
        tvName = findViewById(R.id.tv_name);
        tvSinger = findViewById(R.id.tv_singer);
        tvPlayTime = findViewById(R.id.tv_play_time);
        tvAllTime = findViewById(R.id.tv_all_time);
        btLast = findViewById(R.id.bt_last);
        btPlay = findViewById(R.id.bt_play);
        btNext = findViewById(R.id.bt_next);
        seekBar = findViewById(R.id.seek_bar);
        Glide.with(this).load(imageUrl).into(imageView);
        setMp3Info(XiaXiaPlayer.getInstance().getCurrentMp3());
        startTimer();

        btPlay.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if (XiaXiaPlayer.getInstance().isPlaying()) {
                    btPlay.setText("播放");
                    XiaXiaPlayer.getInstance().pause();
                    stopTimer();
                } else {
                    XiaXiaPlayer.getInstance().start();
                    startTimer();
                }
            }
        });
        btLast.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                XiaXiaPlayer.getInstance().playLast();
                setMp3Info(XiaXiaPlayer.getInstance().getCurrentMp3());
                startTimer();
            }
        });
        btNext.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                XiaXiaPlayer.getInstance().playNext();
                setMp3Info(XiaXiaPlayer.getInstance().getCurrentMp3());
                startTimer();
            }
        });

        seekBar.setOnSeekBarChangeListener(new SeekBar.OnSeekBarChangeListener() {
            @Override
            public void onProgressChanged(SeekBar seekBar, int progress, boolean fromUser) {
                if (!fromUser) {
                    return;
                }
                tvPlayTime.setText(Mp3Manage.toTime(progress));
            }

            @Override
            public void onStartTrackingTouch(SeekBar seekBar) {
                isTrackingTouch = true;
            }

            @Override
            public void onStopTrackingTouch(SeekBar seekBar) {
                XiaXiaPlayer.getInstance().seekTo(seekBar.getProgress());
                isTrackingTouch = false;
            }
        });
    }

    private void setMp3Info(Mp3 mp3) {
        if (mp3 == null) {
            return;
        }
        tvName.setText(mp3.name);
        tvSinger.setText(mp3.artist);
        tvAllTime.setText(mp3.time);
        tvPlayTime.setText(Mp3Manage.toTime(XiaXiaPlayer.getInstance().getCurrentPosition()));
    }

    private void startTimer() {
        btPlay.setText("暂停");
        int duration = XiaXiaPlayer.getInstance().getDuration();
        seekBar.setMax(duration);
        RotateAnimation animation = new RotateAnimation(0f, 360f, Animation.RELATIVE_TO_SELF, 0.5f, Animation.RELATIVE_TO_SELF, 0.5f);
        animation.setInterpolator(new LinearInterpolator());
        animation.setDuration(10000);//设置动画持续周期
        animation.setRepeatCount(-1);//设置重复次数
        animation.setFillAfter(true);//动画执行完后是否停留在执行完的状态
        imageView.startAnimation(animation);

        if (countDownTimer != null) {
            countDownTimer.cancel();
        }
        countDownTimer = new CountDownTimer(10 * 60 * 1000, 500) {
            @Override
            public void onTick(long millisUntilFinished) {
                if (isTrackingTouch) {
                    return;
                }
                seekBar.setProgress(XiaXiaPlayer.getInstance().getCurrentPosition());
                tvPlayTime.setText(Mp3Manage.toTime(XiaXiaPlayer.getInstance().getCurrentPosition()));
            }

            @Override
            public void onFinish() {

            }
        }.start();
        btPlay.setText("暂停");
    }

    private void stopTimer() {
        btPlay.setText("播放");
        imageView.clearAnimation();
        if (countDownTimer != null) {
            countDownTimer.cancel();
        }
        countDownTimer = null;
    }

}
